<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> 维保工单 </span>
      </template>
      <!-- <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'maintenance:management:maintenanceWorkTask:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template> -->
      <template #firstColumn="{ record }">
        <a @click="handleForm({ tid: record.tid })">
          {{ record.enterpriseCode }}
        </a>
      </template>
      <template #abnormalPic="{ record }">
        <Image v-if="record.abnormalPic" :src="record.abnormalPic" width="100" height="100" alt="" />
        <div style="height: 100px;text-align: center;line-height: 100px;color:#32CD32" v-else>
          <img src="/@/assets/images/noData.png" style="width: 150px;height:auto;" alt="">
        </div>
      </template>
      <template #completeUrl="{ record }">
        <Image style="height: 100px" v-if="record.completeUrl" :src="record.completeUrl" width="100" height="100" alt="" />
        <div style="height: 100px;text-align: center;line-height: 100px;color:#32CD32" v-else>
          <img src="/@/assets/images/noData.png" style="width: 150px;height:auto;" alt="">
        </div>
      </template>
      <!-- 新增 action 插槽 -->
      <template #action="{ record }">
        <a-button @click="handleForm({ tid: record.tid })" type="text">
          <span style="font-size: 14px; color: #4169E1;">详情</span>
        </a-button>
        <Popconfirm
          title="是否确认删除该维保工单"
          ok-text="确认"
          cancel-text="取消"
          @confirm="handleDelete(record)"
        >
          <a-button 
            v-auth="'maintenance:management:maintenanceWorkTask:edit'" 
            type="text" 
            style="color: red;"
          >
            <span style="font-size: 14px;">删除</span>
          </a-button>
        </Popconfirm>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" /> 
  </div>
</template>
<script lang="ts" setup name="ViewsMaintenanceManagementMaintenanceWorkTaskList">
  import { unref, ref, onMounted, } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { maintenanceWorkTaskDelete, maintenanceWorkTaskListData } from '/@/api/maintenance.management/maintenanceWorkTask';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import { officeTreeData } from '/@/api/sys/office';
  import { inDeviceManagementListData } from '/@/api/device.management/inDeviceManagement';
  import { unitEnterpriseDominantListData } from '/@/api/unitmanage/unitEnterpriseDominant';
  import InputForm from './form.vue';
  import { Image } from 'ant-design-vue';
  import { Popconfirm } from 'ant-design-vue'; // 引入 Popconfirm 组件
 

  const { t } = useI18n('maintenance.management.maintenanceWorkTask');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const route = router.currentRoute;
 

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('维保工单流程记录表管理'),
  };
  const maintenanceWorkTaskParams = ref({
    name: ''
  });
  const EnterpriseParams = ref<Recordable>({
    name: ''
  });
  const FireHazardTaskId = ref('');

  const searchForm: FormProps = {
    baseColProps: { md: 8, lg: 4 },
    labelWidth: 90,
    schemas: [
      {
        label: t(''),
        field: 'enterpriseCode',
        fieldLabel: 'enterpriseName',
        component: 'TreeSelect',
        componentProps: () => {
          return {
            placeholder: t('请输入所属主体'),
            api: unitEnterpriseDominantListData,
            params: EnterpriseParams.value,
            fieldNames: { label: 'name', value: 'unitCode' },
            resultField: 'list',
            notFoundContent: '未查询到记录',
            filterTreeNode: false,
            allowClear: true,
            filterOption: false,
            immediate: true,
            onSearch: (value: string) => {
              EnterpriseParams.value.name = value;
              console.log(EnterpriseParams.value);
            },
          };
        },
      },
      {
        label: t(''),
        field: 'equipmentName',
        component: 'Input',
        componentProps: {
          placeholder: t('请输入设备名称')
        },
      },
      {
        label: t(''),
        field: 'id',
        fieldLabel: 'name',
        component: 'Select',
        componentProps: () => {
          return {
            placeholder: t('请输入维保工单'),
            api: maintenanceWorkTaskListData,
            params: maintenanceWorkTaskParams.value,
            fieldNames: { label: 'name', value: 'id' },
            resultField: 'list',
            notFoundContent: '未查询到记录',
            filterTreeNode: false,
            allowClear: true,
            filterOption: false,
            immediate: true,
            onSearch: (value: string) => {
              maintenanceWorkTaskParams.value.name = value;
              console.log(maintenanceWorkTaskParams.value);
            },
          };
        },
      },
      {
        label: t(''),
        field: 'reportPersonName',
        component: 'Input',
        componentProps: {
          placeholder: t('请输入上报人')
        },
      },
      {
        label: t(''),
        field: 'status',
        component: 'Select',
        componentProps: {
          placeholder: t('维保工单状态'),
          options: [
            { label: t('已完成'), value: '已完成' },
            { label: t('未完成'), value: '未完成' }
          ]
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('所属主体'),
      dataIndex: 'enterpriseName',
      key: 'a.enterprise_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('设备名称'),
      dataIndex: 'equipmentName',
      key: 'a.equipment_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('工单名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('地点'),
      dataIndex: 'address',
      key: 'a.address',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('异常图片'),
      dataIndex: 'abnormalPic',
      key: 'a.abnormal_pic',
      sorter: true,
      width: 130,
      align: 'left',
      slots: { customRender: 'abnormalPic' },
    },
    {
      title: t('维保企业名称'),
      dataIndex: 'maintenanceName',
      key: 'a.maintenance_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('上报人'),
      dataIndex: 'reportPersonName',
      key: 'a.report_person_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    // {
    //   title: t('上报人编码'),
    //   dataIndex: 'reportPersonCode',
    //   key: 'a.report_person_code',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    {
      title: t('上报时间'),
      dataIndex: 'reportTime',
      key: 'a.report_time',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('执行人'),
      dataIndex: 'executePerson',
      key: 'a.execute_person',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('工单状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'maintenance_work_task_state'
    },
    {
      title: t('完成图片'),
      dataIndex: 'completeUrl',
      key: 'a.complete_url',
      sorter: true,
      slots: { customRender: 'completeUrl' },
      width: 130,
      align: 'left',
    },
    {
      title: t('操作'),
      dataIndex: 'action',
      key: 'action',
      width: 220,
      fixed: 'right',
      slot: 'action'
    }
  ];

  const [registerTable, { reload }] = useTable({
    api: maintenanceWorkTaskListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: null, // 移除原 actionColumn
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  const [registerDrawer, { openDrawer }] = useDrawer();

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { tid: record.tid };
    const res = await maintenanceWorkTaskDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    traceModel(true, { formKey: 'maintenance_process', bizKey: record.taskId });
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }



  onMounted(() => {
    const tid = unref(route).query.tid;
    if (tid) {
      handleForm({ tid });
    }
  });
</script>    